<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <common-item1
          :query="query"
          :streetList="streetList"
          :communityList="communityList"
          :villageList="villageList"
          @visibleChangeStreet="visibleChangeStreet"
          @changeStreet="changeStreet"
          @visibleChangeCommunity="visibleChangeCommunity"
          @changeCommunity="changeCommunity"
          @visibleChangeVillage="visibleChangeVillage"
          @changeVillage="changeVillage"
          :show="false"
          :showInput="showInput"
          @search="handleSearch"
        />

        <el-select
          v-model="query.orderStatus"
          placeholder="订单状态"
          class="handle-select mr10"
          clearable
        >
          <el-option key="0" label="预约成功" value="0"></el-option>
          <el-option key="1" label="已取消" value="1"></el-option>
          <el-option key="2" label="已完成" value="2"></el-option>
        </el-select>
        <el-input
          v-model="query.mobile"
          placeholder="联系电话"
          class="handle-input mr10"
        ></el-input>
        <div class="block" style="float: left; margin-right: 10px;">
          <el-date-picker
            v-model="value2"
            type="daterange"
            align="left"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button type="primary" class="handle-del mr10" @click="handleExport"
          >导出</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="recycleDetails"
          align="center"
          label="回收详情"
        ></el-table-column>
        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">{{
            scope.row.orderStatus == 0
              ? "预约成功"
              : scope.row.orderStatus == 1
              ? "已取消"
              : "已完成"
          }}</template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="consignee"
          label="联系人"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="mobile"
          label="联系电话"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="subDistrict"
          label="街道"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="communityName"
          label="社区"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="villageName"
          label="小区"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="address"
          label="详细地址"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="startDate"
          label="预约开始时间"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="endDate"
          label="预约结束时间"
        ></el-table-column>

        <el-table-column
          align="center"
          prop="createDate"
          label="下单时间"
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleCancel(scope.$index, scope.row)"
              >取消</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleComplete(scope.$index, scope.row)"
              >完成</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <!-- <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="用户名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
import { commonItem1 } from "../../common/commonItem1/common-item1.js";
import NProgress from "nprogress";
export default {
  name: "basetable",
  data() {
    return {
      query: {
        pageIndex: 1,
        pageSize: 20,
        orderStatus: "",
        streetId: "",
        communityId: "",
        villageId: "",
        mobile: "",
        // startDate: "",
        // endDate: "",
      },
      value2: "",
      tableData: [],
      showInput: true,
      streetList: [],
      communityList: [],
      villageList: [],
      //   multipleSelection: [],
      delList: [],
      editVisible: false,
      pageTotal: 0,
      //   form: {},
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getData();
  },
  components: {
    "common-item1": () => import("../../common/commonItem1/common-item1.vue"),
  },
  methods: {
    ...commonItem1,
    getData() {
      this.$axios
        .get("/order/recycleOrder/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
            orderStatus: this.query.orderStatus,
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            mobile: this.query.mobile,
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
          this.tableData.map((item, index) => {
            item.createDate = this.dateFormat(item.createDate);
            item.startDate = this.dateFormat(item.startDate);
            item.endDate = this.dateFormat(item.endDate);
            item.recycleDetails = item.recycleDetails.join("、");
          });
          console.log(res);
          this.pageTotal = res.data.total;
        })
        .catch(() => {});
    },
    handleExport() {
      if (
        this.query.orderStatus == "" &&
        this.query.streetId === "" &&
        this.query.communityId == "" &&
        this.query.villageId == "" &&
        this.query.mobile == "" &&
        this.value2 == ""
      ) {
        return this.$message.warning({
          message: "请选择导出条件",
          duration: 1500,
        });
      }

      NProgress.start();
      this.$axios
        .get("/order/recycleOrder/export", {
          params: {
            orderStatus: this.query.orderStatus,
            subDistrictId: this.query.streetId,
            communityId: this.query.communityId,
            villageId: this.query.villageId,
            mobile: this.query.mobile,
            startDate: this.value2 ? this.value2[0].getTime() : "",
            endDate: this.value2
              ? (
                  this.value2[1].getTime() +
                  (24 * 3600 * 1000 - 1000)
                ).toString()
              : "",
          },
          responseType: "blob",
        })
        .then((res) => {
          if (res.data.type != "application/vnd.ms-excel") {
            NProgress.done();
            return this.$message.warning({
              message: res.msg,
              duration: 1500,
            });
          }

          // 导出excel表格
          const link = document.createElement("a");
          let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          let num = "";
          for (let i = 0; i < 10; i++) {
            num += Math.ceil(Math.random() * 10);
          }
          link.setAttribute("download", "用户_" + num + ".xls");
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);

          NProgress.done();
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    handleCancel(index, row) {
      this.$confirm("确定要取消吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.query.orderStatus = 1;
          this.edit(row.id);
        })
        .catch(() => {});
    },
    handleComplete(index, row) {
      this.$confirm("确定要完成吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          console.log(index, row);
          this.query.orderStatus = 2;
          this.edit(row.id);
        })
        .catch(() => {});
    },
    edit(id) {
      this.$axios
        .put("/order/recycleOrder", {
          id,
          orderStatus: this.query.orderStatus,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.query.orderStatus = "";
          this.getData();
        })
        .catch(() => {});
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.$set(this.query, "pageSize", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
